package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sports;
import com.itheima.service.SportsService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/sports")
public class SportsController {
    @Reference
    private SportsService sportsService;

    @RequestMapping("/upload") //文件上传
    public Result upload(MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() != 0) {
                for (String[] strings : list) {
                    Sports sports = new Sports(strings[0],strings[1],strings[2],strings[3],Integer.parseInt(strings[4].trim()),Integer.parseInt(strings[5].trim()));
                    sportsService.add(sports);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_SPORTS_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_SPORTS_SUCCESS);
    }

    @RequestMapping("/findPage") //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return sportsService.findPage(queryPageBean);
    }

    @RequestMapping("/add") //单项添加
    public Result add(@RequestBody Sports sports) {
        try {
            sportsService.add(sports);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_KNOWLEDGE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_KNOWLEDGE_SUCCESS);
    }

    @RequestMapping("/delete") //删除
    public Result delete(Integer id) {
        try {
            sportsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_KNOWLEDGE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_KNOWLEDGE_SUCCESS);
    }

//    @RequestMapping("/findById") //修改界面数据回显
//    public Result findById(Integer id) {
//        Sports sports = null;
//        try {
//            sports = sportsService.findById(id);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.GET_KNOWLEDGE_FAIL);
//        }
//        return new Result(true, MessageConstant.GET_KNOWLEDGE_SUCCESS, sports);
//    }

    @RequestMapping("/edit") //修改
    public Result edit(@RequestBody Sports sports) {
        System.out.println(sports.getMinAge()+"..."+sports.getMaxAge()+"..."+sports.getAge());
        try {
            sportsService.edit(sports);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_KNOWLEDGE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_KNOWLEDGE_SUCCESS);
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Sports> list =  sportsService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}

